package com.lunwen.lunwen.api;

import com.lunwen.lunwen.bean.LunWenXinXi;
import com.lunwen.lunwen.bean.MuBan;
import com.lunwen.lunwen.bean.Result;
import com.lunwen.lunwen.services.MuBanService;
import com.lunwen.lunwen.services.XueYuanService;
import com.lunwen.lunwen.services.ZhuanYeService;
import org.apache.ibatis.annotations.Param;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import org.springframework.beans.factory.annotation.Autowired;

import java.util.*;

/**
 * (MuBan)表控制层
 *
 * @author makejava
 * @since 2020-07-24 12:30:06
 */
@RestController
@RequestMapping("api")
public class MuBanController {

    /**
     * 服务对象
     */
    @Autowired
    private MuBanService muBanService;

    @Autowired
    private XueYuanService xueYuanService;

    @Autowired
    private ZhuanYeService zhuanYeService;

    @RequestMapping("insertmuban")
    public Result insertMuBan(@RequestBody MuBan muBan){
        Result result = new Result();
        if (StringUtils.isEmpty(muBan.getMubanmingcheng())){
            result.setResult("201","模板名称不能为空");
            return result;
        }
        if (!StringUtils.isEmpty(muBan.getXueyuanId())){
            if (xueYuanService.queryById(muBan.getXueyuanId()) == null){
                result.setResult("201","该学院不存在！");
                return result;
            }
        }
        if (!StringUtils.isEmpty(muBan.getZhuanyeId())){
            if (zhuanYeService.queryById(muBan.getZhuanyeId()) == null){
                result.setResult("201","该专业不存在！");
                return result;
            }
            if (!zhuanYeService.queryById(muBan.getZhuanyeId()).getXueyuanId().equals(muBan.getXueyuanId())){
                result.setResult("201","该学院不存在该专业！");
                return result;
            }
        }
        muBan.setMubanId(UUID.randomUUID().toString());
        muBan.setZhuangtai(1);
        muBan.setGengxinshijian(new Date());
        MuBan insert = muBanService.insert(muBan);
        result.setResult("200","添加成功！",insert);
        return result;
    }

    @RequestMapping("updatemuban")
    public Result updatetMuBan(@RequestBody MuBan muBan){
        Result result = new Result();
        if (StringUtils.isEmpty(muBan.getMubanId())){
            result.setResult("201","模板ID不能为空！");
            return result;
        }
        if (muBanService.queryById(muBan.getMubanId()) == null){
            result.setResult("201","不存在该模板信息！");
            return result;
        }
        if (StringUtils.isEmpty(muBan.getMubanmingcheng())){
            result.setResult("201","模板名称不能为空");
            return result;
        }
        if (!StringUtils.isEmpty(muBan.getXueyuanId())){
            if (xueYuanService.queryById(muBan.getXueyuanId()) == null){
                result.setResult("201","该学院不存在！");
                return result;
            }
        }
        if (!StringUtils.isEmpty(muBan.getZhuanyeId())){
            if (zhuanYeService.queryById(muBan.getZhuanyeId()) == null){
                result.setResult("201","该专业不存在！");
                return result;
            }
            if (!zhuanYeService.queryById(muBan.getZhuanyeId()).getXueyuanId().equals(muBan.getXueyuanId())){
                result.setResult("201","该学院不存在该专业！");
                return result;
            }
        }
        muBan.setGengxinshijian(new Date());
        MuBan update = muBanService.update(muBan);
        result.setResult("200","修改成功！",update);
        return result;
    }

    @RequestMapping("selectonemuban")
    public Result selectOneMuBan(@RequestParam("muBanId") String muBanId){
        Result result = new Result();
        MuBan muBan = muBanService.queryById(muBanId);
        if (muBan == null){
            result.setResult("201","不存在该模板信息！");
            return result;
        }
        result.setResult("200","查询成功！",muBan);
        return result;
    }

    @RequestMapping("selectallmuban")
    public Result selectAllMuBan(@RequestParam("yeMa") int yeMa,
                                 @RequestParam(required = false) String mubanmingcheng,
                                 @RequestParam(required = false) String xueyuanId,
                                 @RequestParam(required = false) String zhuanyeId,
                                 @RequestParam(required = false) Integer zhuangtai,
                                 @RequestParam(required = false) String shuoming){
        Result result = new Result();
        MuBan muBan = new MuBan();
        muBan.setMubanmingcheng(mubanmingcheng);
        muBan.setXueyuanId(xueyuanId);
        muBan.setZhuanyeId(zhuanyeId);
        muBan.setZhuangtai(zhuangtai);
        muBan.setShuoming(shuoming);
        int limit = 10;
        int zongGeShu = muBanService.queryCount(muBan);
        int xiaBiao = (yeMa - 1) * limit;
        int zongYeMa = (zongGeShu - 1) / limit + 1;
        List<MuBan> muBans = muBanService.queryAllByLimit(muBan, xiaBiao, limit);
        Map<String,Object> map = new HashMap<String, Object>();
        map.put("zongGeShu", zongGeShu);
        map.put("zongYeMa", zongYeMa);
        map.put("list", muBans);
        result.setResult("200","查询成功！",map);
        return result;
    }

}